<?php

/**
 * @author Djordje Zeljic
 * Date: Jun 23, 2010 4:22:51 PM
 */
class image_m extends Model {

    var $errors = '';

    function image_m () {
        parent::Model();
    }// construct of image_m

    function create($type) {
        $config = array();
        $config['upload_path'] = './assets/img/'.$type;
        $config['allowed_types'] = 'jpg';
        $config['max_size']	= '800';
        $config['max_width'] = '2048';
        $config['max_height'] = '1536';
        $config['file_name'] = time().'_'.$this->input->post(substr($type,0,strlen($type)-1).'_id');
        $config['overwrite'] = TRUE;
        $this->firephp->fb('p: '.$this->input->post(substr($type,0,strlen($type)-1).'_id'));
        $this->upload->initialize($config);

        if($this->upload->do_upload('newpic')) {
            // if uploaded do resize
            $data = $this->upload->data();
            $iconfig['image_library'] = 'gd2';
            $iconfig['source_image']	= $data['full_path'];
            $iconfig['create_thumb'] = FALSE;
            $iconfig['maintain_ratio'] = FALSE;
            $iconfig['width']	 = 200;
            $iconfig['height']	= 144;

            $this->image_lib->initialize($iconfig);
            $this->image_lib->resize();
            
            
            
            switch ($type) {
               case 'excursions':
               $data = array('url'=>$config['file_name'],'excursions_id'=>$this->input->post('excursion_id'));
                $this->db->insert('excimg',$data);
                 break;
               case 'tours':
               $data = array('url'=>$config['file_name'],'tours_id'=>$this->input->post('tour_id'));
               $this->db->insert('tourimg',$data);
               break;
            
            }
            
            

            return array('success'=>'success','data'=>$data,'message'=>'Dodavanje slike je uspjesno zavrseno!');
        }else {
            return array('success'=>'failed','message'=>$this->upload->display_errors(' ','<br />'));
        }

    }

}
?>